﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.RobotJog.CApRJRobotJogClipboard
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using EngineeringInternalExtension;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using Tecnomatix.Engineering;


namespace DnProcessSimulateCommands.RobotJog;

public class CApRJRobotJogClipboard
{
  private ITxRoboticLocationOperation _location;
  private TxTransformation _locationOriginalTransformation;
  private TxTransformation _locationOriginalTaughtTransformation;
  private TxRobotConfigurationData _locationOriginalTaughtRobotConfigurationData;
  private TxRobot _robot;
  private CApRJRobotState _originalRobotState;

  public ITxRoboticLocationOperation Location
  {
    get => this._location;
    set
    {
      this._location = value;
      if (this._location != null)
      {
        this._locationOriginalTransformation = (this._location as ITxLocatableObject).AbsoluteLocation;
        this.Robot = this._location.ParentRoboticOperation.Robot as TxRobot;
        this._locationOriginalTaughtTransformation = this._location.GetTaughtPosition();
        this._locationOriginalTaughtRobotConfigurationData = this._location.RobotConfigurationData;
      }
      this._originalRobotState = this.GetRobotAndLocationState();
    }
  }

  public TxRobot Robot
  {
    get => this._robot;
    set
    {
      this._robot = value;
      this.IsRobotBaseAtAbsoluteLocation = TxTransformation.op_Equality(this._robot.AbsoluteLocation, this._robot.Baseframe.AbsoluteLocation);
      this.IsRobotPositionReadOnly = TxComponentEx.IsScopeReadOnly((ITxObject) this._robot);
      if (TxObjectEx.GetLowestLevelNonModelObject((ITxObject) this._robot) is ITxComponent levelNonModelObject && levelNonModelObject.IsEquipment)
        this.Equipment = levelNonModelObject;
      this.UpdateRobotAttachment();
      this._originalRobotState = this.GetRobotAndLocationState();
    }
  }

  public bool IsRobotBaseAtAbsoluteLocation { get; private set; }

  public bool IsRobotPositionReadOnly { get; private set; }

  public ITxComponent Equipment { get; private set; }

  public ITxLocatableObject RobotAttachment { get; private set; }

  public List<ITxLocatableObject> RobotAttachmentsChain { get; set; }

  public bool IsAttachmentChanged
  {
    get
    {
      List<ITxLocatableObject> attachmentChain = CApRJRobotJogUtilities.GetAttachmentChain((ITxLocatableObject) this.Robot);
      if (TxRobot.op_Inequality(this.Robot, (ITxObject) null))
        attachmentChain.Insert(0, (ITxLocatableObject) this.Robot);
      return this.RobotAttachmentsChain.IsDifferent<ITxLocatableObject>((IEnumerable<ITxLocatableObject>) attachmentChain);
    }
  }

  public CApRJRobotJogClipboard() => this.RobotAttachmentsChain = new List<ITxLocatableObject>();

  public CApRJRobotState GetRobotAndLocationState()
  {
    CApRJRobotState andLocationState = new CApRJRobotState();
    if (TxRobot.op_Inequality(this.Robot, (ITxObject) null))
    {
      andLocationState.RobotTransformation = this.Robot.AbsoluteLocation;
      andLocationState.RobotPoses = new Dictionary<ITxDevice, TxPoseData>()
      {
        {
          (ITxDevice) this.Robot,
          new TxPoseData(((ITxDevice) this.Robot).CurrentPose)
        }
      };
      foreach (TxJoint externalAx in (Collection<ITxObject>) this.Robot.ExternalAxes)
      {
        if (!andLocationState.RobotPoses.ContainsKey(externalAx.Device))
          andLocationState.RobotPoses.Add(externalAx.Device, new TxPoseData(externalAx.Device.CurrentPose));
      }
      if (this.RobotAttachment != null)
        andLocationState.AttachmentTransformation = this.RobotAttachment.AbsoluteLocation;
    }
    if (this.Equipment != null)
      andLocationState.EquipmentTransformation = (this.Equipment as ITxLocatableObject).AbsoluteLocation;
    if (this.Location != null)
    {
      andLocationState.LocationTransformation = (this.Location as ITxLocatableObject).AbsoluteLocation;
      if (ITxRoboticLocationOperationEx.GetTaughtPose(this.Location) != null)
        andLocationState.LocationTaughtPose = ITxRoboticLocationOperationEx.GetTaughtPose(this.Location);
    }
    return andLocationState;
  }

  public void Reset()
  {
    this.ResetRobot();
    this.ResetLocatableObject();
  }

  public void ResetPositions()
  {
    if (TxRobot.op_Inequality(this.Robot, (ITxObject) null) || this._originalRobotState != null)
    {
      if (this.Equipment != null)
        (this.Equipment as ITxLocatableObject).AbsoluteLocation = this._originalRobotState.EquipmentTransformation;
      else if (!this.IsRobotPositionReadOnly)
      {
        if (this.RobotAttachment != null)
          this.RobotAttachment.AbsoluteLocation = this._originalRobotState.AttachmentTransformation;
        this.Robot.AbsoluteLocation = this._originalRobotState.RobotTransformation;
      }
    }
    this.ResetLocatableObject();
    this.ResetLocationTaughtParameters();
  }

  public void ResetRobot()
  {
    if (!TxRobot.op_Inequality(this.Robot, (ITxObject) null))
      return;
    this.SetRobotState(this._originalRobotState);
  }

  public void ResetLocationTaughtParameters()
  {
    if (this.Location == null)
      return;
    this.Location.Teach(this._locationOriginalTaughtTransformation, this._locationOriginalTaughtRobotConfigurationData);
  }

  public void ResetLocatableObject()
  {
    if (this.Location == null)
      return;
    this.SetLocationState(this._originalRobotState);
  }

  public void ResetLocation()
  {
    if (this.Location == null)
      return;
    (this.Location as ITxLocatableObject).AbsoluteLocation = this._locationOriginalTransformation;
  }

  public void SetRobotState(CApRJRobotState robotState)
  {
    if (TxRobot.op_Equality(this.Robot, (ITxObject) null) || this._originalRobotState == null)
      return;
    if (robotState.RobotPoses != null)
    {
      foreach (KeyValuePair<ITxDevice, TxPoseData> robotPose in robotState.RobotPoses)
        robotPose.Key.CurrentPose = robotPose.Value;
    }
    if (this.Equipment != null)
    {
      (this.Equipment as ITxLocatableObject).AbsoluteLocation = robotState.EquipmentTransformation;
    }
    else
    {
      if (this.IsRobotPositionReadOnly)
        return;
      if (this.RobotAttachment != null)
        this.RobotAttachment.AbsoluteLocation = robotState.AttachmentTransformation;
      this.Robot.AbsoluteLocation = robotState.RobotTransformation;
    }
  }

  public void SetLocationState(CApRJRobotState robotState)
  {
    if (this.Location == null || robotState == null)
      return;
    (this.Location as ITxLocatableObject).AbsoluteLocation = robotState.LocationTransformation;
    if (robotState.LocationTaughtPose == null || !this.Robot.UsesNumericInverse)
      return;
    ITxRoboticLocationOperationEx.SetTaughtPose(this.Location, robotState.LocationTaughtPose);
  }

  public void SetRobotAndLocationState(CApRJRobotState robotState)
  {
    this.SetRobotState(robotState);
    this.SetLocationState(robotState);
  }

  public void UpdateRobotAttachment()
  {
    this.RobotAttachmentsChain = CApRJRobotJogUtilities.GetAttachmentChain((ITxLocatableObject) this.Robot);
    this.RobotAttachment = CApRJRobotJogUtilities.GetTopMostRobotAttachment(this.RobotAttachmentsChain);
    if (this.RobotAttachment != null)
    {
      if (TxComponentEx.IsScopeReadOnly((ITxObject) this.RobotAttachment))
        this.RobotAttachment = (ITxLocatableObject) null;
      if (this.RobotAttachment != null)
        this._originalRobotState = this.GetRobotAndLocationState();
    }
    if (!TxRobot.op_Inequality(this.Robot, (ITxObject) null))
      return;
    this.RobotAttachmentsChain.Insert(0, (ITxLocatableObject) this.Robot);
  }
}
